//
//  TTTabBarController.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2018/12/17.
//  Copyright © 2018 Gox_WallyDiao. All rights reserved.
//

import UIKit

class TTTabBarController: UITabBarController, UITabBarControllerDelegate {

    //  MARK: - ###### 产品寿命 ######
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        self.tabBar.isTranslucent = false
        self.tabBar.tintColor = TabbarTintColor
        
        //设置子视图控制器
        renderChildController()
        
        AppDelegate.getAppDelegate().tabbarVC = self
        self.delegate = self
        
        
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //  MARK: - ###### 子视图 ######
    func renderChildController() {

        let homeVc = loadChildViewController(viewController_name: "HomeViewController", title: "首页", unselect_image: "icon_first", selected_image: "icon_first_hover")
        let welfareVc = loadChildViewController(viewController_name: "WelfareViewController", title: "红包", unselect_image: "icon_redPacket", selected_image: "icon_redPacket")
        let listenVc = loadChildViewController(viewController_name: "ListenViewController", title: "天天听", unselect_image: "icon_second", selected_image: "icon_second_hover")
        let recordVc = loadChildViewController(viewController_name: "RecordViewController", title: "记录", unselect_image: "icon_second", selected_image: "icon_second_hover")
        let redPacketVc = loadChildViewController(viewController_name: "RedPacketViewController", title: "红包", unselect_image: "icon_third", selected_image: "icon_third_hover")
        let mineVc = loadChildViewController(viewController_name: "MineViewController", title: "我的", unselect_image: "icon_fourth", selected_image: "icon_fourth_hover")
        self.viewControllers = [homeVc!,welfareVc!, recordVc!, mineVc!]
        
    }

}

//  MARK: - ###### 协议 ######
extension TTTabBarController {
    //TODO: UITabBarControllerDelegate
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        return true
    }
}

//  MARK: - ###### 事件处理 ######
extension TTTabBarController {}

//  MARK: - ###### 私有方法 ######
extension TTTabBarController {
    func loadChildViewController(viewController_name: String?, title: String, unselect_image: String, selected_image: String) -> UIViewController? {
        
        let vc:UIViewController?
        vc = loadViewController(viewControllerName: viewController_name!)
        
        let nvc = TTBaseNavigationController(rootViewController: vc!)
        nvc.title = title
        var textAttributes = [NSAttributedString.Key : Any]()
        let foregroundColorKey = NSAttributedString.Key.foregroundColor
        textAttributes[foregroundColorKey] = BlackBackColor
        var textAttributes1 = [NSAttributedString.Key : Any]()
        let foregroundColorKey1 = NSAttributedString.Key.foregroundColor
        textAttributes1[foregroundColorKey1] = TabbarTintColor
        nvc.tabBarItem.setTitleTextAttributes(textAttributes, for: .normal)
        nvc.tabBarItem.setTitleTextAttributes(textAttributes1, for: .selected)
        nvc.tabBarItem.image = UIImage(named: unselect_image)?.withRenderingMode(.alwaysOriginal)
        nvc.tabBarItem.selectedImage = UIImage(named: selected_image)?.withRenderingMode(.alwaysOriginal)
        return nvc
        
    }
    
}

//  MARK: - ###### 公有方法 ######
extension TTTabBarController {
    func setControllerItem() {
        
        
    }
}
